package com.ybkj.daijia.server.event.listener;

import com.alibaba.fastjson.JSONObject;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.event.model.DriverCreateEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class DriverCreateEventListener implements ApplicationListener<DriverCreateEvent> {

    private static final Logger logger = LoggerFactory.getLogger(DriverCreateEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;
    @Autowired
    private SmsService smsService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Override
    @Async
    public void onApplicationEvent(DriverCreateEvent event) {

        Driver driver = (Driver) event.getSource();

        int ret = -99;
        try {
            String password = driver.getPassword();

            String chenghu = "先生";
            if (driver.getGender().equals("女")) {
                chenghu = "女士";
            }

            SmsTemplet templet = smsService
                .findByAlias(driver.getCompanyId(), SmsType.driverCreate);
            Company driverCompany = companyService.findOne(driver.getCompanyId());
            Company finalCompany = driverCompany;
            Company main = companyService.getMain();
            Setting setting = settingUtils.get();
            if (!main.getId().equals(driverCompany.getId()) && setting.isSmsMainCompany()) {
                finalCompany = main;
            }
            if (null != templet && StringUtils.isNotBlank(templet.getContent())) {
                //ret=smsService.sendSms(driver.getPhone(), "driverCreate", "{\"服务人员名\":\""+driver.getName()+"\",\"称呼\":\""+chenghu+"\",\"公司名\":\""+driverCompany.getAbbreviation()+"\",\"服务人员账号\":\""+driver.getUsername()+"\",\"密码\":\""+password+"\"}", finalCompany.getId(), 0, finalCompany.getSignName());
                //短信发送（改）
                JSONObject smsParam = new JSONObject();
                smsParam.put("name", driver.getName());
                smsParam.put("company", driverCompany.getAbbreviation());
                smsParam.put("user", driver.getUsername());
                smsParam.put("password", password);
                smsService
                    .sendSms(driver.getPhone(), templet.getTemplateId(), smsParam.toJSONString(),
                        finalCompany.getId(), 0, finalCompany.getSignName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.debug("短信结果：" + ret);
    }

}
